package hw2;

public class crossOver {

	int k;
	
	Learner getBestMethod(String inputString) {
		String stringArray[] = new String[5];
		stringArray[0] = inputString.substring(0, inputString.length() / 5);
		stringArray[1] = inputString.substring(inputString.length() / 5, 2 * inputString.length() / 5);
		stringArray[2] = inputString.substring(2 * inputString.length() / 5, 3 * inputString.length() / 5);
		stringArray[3] = inputString.substring(3 * inputString.length() / 5, 4 * inputString.length() / 5);
		stringArray[4] = inputString.substring(4 * inputString.length() / 5, inputString.length());
	
		Learner bestLearner = new Learner();
		double bestResult = 0;
		
		for (int i = 0; i < 5; i++) {
			String testString = stringArray[i];
			String trainingString = "";
			for (int j = 0; j < 5; j++) 
				if (j != i)
					trainingString += stringArray[j];
			
			Learner unigramLearner = new Learner();
			Learner bigramLearner = new Learner();
			Learner trigramLearner = new Learner();
			
			unigramLearner.train(trainingString);
			bigramLearner.train(trainingString);
			trigramLearner.train(trainingString);
			
			double unigramResult = unigramLearner.unigramTest(testString, false);
			double bigramResult = bigramLearner.bigramTest(testString, false);
			double trigramResult = trigramLearner.trigramTest(testString, false);
			
			double max = Math.max(Math.max(unigramResult, bigramResult),trigramResult);
			
			if (bestResult < max) {
				bestResult = max;
				continue;
			}
			
			if (unigramResult == max)
			{
				bestLearner = unigramLearner;
				bestLearner.setBestMethod(1);
			}
			else if (bigramResult == max)
			{
				bestLearner = bigramLearner;
				bestLearner.setBestMethod(2);
			}
			else
			{
				bestLearner = trigramLearner;
				bestLearner.setBestMethod(3);
			}
		}
		
		return bestLearner;
	
	
	
	}
}
